3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Daniel Kinzler
22 use MediaWiki\Interwiki\InterwikiLookup
;
23 use Wikimedia\TestingAccessWrapper
;
26 * @covers MediaWikiTitleCodec
30 * ^--- needed because of global state in
32 class MediaWikiTitleCodecTest
extends MediaWikiTestCase
{
34 public function setUp() {
37 $this->setMwGlobals( [
38 'wgAllowUserJs' => false,
39 'wgDefaultLanguageVariant' => false,
40 'wgMetaNamespace' => 'Project',
41 'wgLocalInterwikis' => [ 'localtestiw' ],
42 'wgCapitalLinks' => true,
44 $this->setUserLang( 'en' );
45 $this->setContentLang( 'en' );
49 * Returns a mock GenderCache that will consider a user "female" if the
50 * first part of the user name ends with "a".
54 private function getGenderCache() {
55 $genderCache = $this->getMockBuilder( GenderCache
::class )
56 ->disableOriginalConstructor()
59 $genderCache->expects( $this->any() )
60 ->method( 'getGenderOf' )
61 ->will( $this->returnCallback( function ( $userName ) {
62 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ?
'female' : 'male';
69 * Returns a mock InterwikiLookup that only has an isValidInterwiki() method, which recognizes
70 * 'localtestiw' and 'remotetestiw'. All other methods throw.
72 * @return InterwikiLookup
74 private function getInterwikiLookup() : InterwikiLookup
{
75 $iwLookup = $this->createMock( InterwikiLookup
::class );
77 $iwLookup->expects( $this->any() )
78 ->method( 'isValidInterwiki' )
79 ->will( $this->returnCallback( function ( $prefix ) {
80 return $prefix === 'localtestiw' ||
$prefix === 'remotetestiw';
83 $iwLookup->expects( $this->never() )
84 ->method( $this->callback( function ( $name ) {
85 return $name !== 'isValidInterwiki';
92 * Returns a mock NamespaceInfo that has only the following methods:
95 * * getCanonicalName()
96 * * hasGenderDistinction()
99 * All other methods throw. The only namespaces that exist are NS_SPECIAL, NS_MAIN, NS_TALK,
100 * NS_USER, and NS_USER_TALK. NS_USER and NS_USER_TALK have gender distinctions. All namespaces
103 * @return NamespaceInfo
105 private function getNamespaceInfo() : NamespaceInfo
{
106 $canonicalNamespaces = [
107 NS_SPECIAL
=> 'Special',
111 NS_USER_TALK
=> 'User_talk',
114 $nsInfo = $this->createMock( NamespaceInfo
::class );
116 $nsInfo->method( 'exists' )
117 ->will( $this->returnCallback( function ( $ns ) use ( $canonicalNamespaces ) {
118 return isset( $canonicalNamespaces[$ns] );
121 $nsInfo->method( 'getCanonicalName' )
122 ->will( $this->returnCallback( function ( $ns ) use ( $canonicalNamespaces ) {
123 return $canonicalNamespaces[$ns] ??
false;
126 $nsInfo->method( 'hasGenderDistinction' )
127 ->will( $this->returnCallback( function ( $ns ) {
128 return $ns === NS_USER ||
$ns === NS_USER_TALK
;
131 $nsInfo->method( 'isCapitalized' )->willReturn( true );
133 $nsInfo->expects( $this->never() )->method( $this->anythingBut(
134 'exists', 'getCanonicalName', 'hasGenderDistinction', 'isCapitalized'
140 protected function makeCodec( $lang ) {
141 return new MediaWikiTitleCodec(
142 Language
::factory( $lang ),
143 $this->getGenderCache(),
145 $this->getInterwikiLookup(),
146 $this->getNamespaceInfo()
150 public static function provideFormat() {
152 [ NS_MAIN
, 'Foo_Bar', '', '', 'en', 'Foo Bar' ],
153 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi Maier#stuff and so on' ],
154 [ false, 'Hansi_Maier', '', '', 'en', 'Hansi Maier' ],
161 'User talk:hansi maier',
162 'User talk:Hansi maier'
165 // getGenderCache() provides a mock that considers first
166 // names ending in "a" to be female.
167 [ NS_USER
, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa Müller' ],
168 [ NS_MAIN
, 'FooBar', '', 'remotetestiw', 'en', 'remotetestiw:FooBar' ],
173 * @dataProvider provideFormat
175 public function testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected,
178 if ( $normalized === null ) {
179 $normalized = $expected;
182 $codec = $this->makeCodec( $lang );
183 $actual = $codec->formatTitle( $namespace, $text, $fragment, $interwiki );
185 $this->assertEquals( $expected, $actual, 'formatted' );
188 $parsed = $codec->parseTitle( $actual, NS_MAIN
);
189 $actual2 = $codec->formatTitle(
190 $parsed->getNamespace(),
192 $parsed->getFragment(),
193 $parsed->getInterwiki()
196 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
199 public static function provideGetText() {
201 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
202 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ],
207 * @dataProvider provideGetText
209 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
210 $codec = $this->makeCodec( $lang );
211 $title = new TitleValue( $namespace, $dbkey, $fragment );
213 $actual = $codec->getText( $title );
215 $this->assertEquals( $expected, $actual );
218 public static function provideGetPrefixedText() {
220 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
221 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ],
223 // No capitalization or normalization is applied while formatting!
224 [ NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
226 // getGenderCache() provides a mock that considers first
227 // names ending in "a" to be female.
228 [ NS_USER
, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
229 [ 1000000, 'Invalid_namespace', '', 'en', 'Special:Badtitle/NS1000000:Invalid namespace' ],
234 * @dataProvider provideGetPrefixedText
236 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
237 $codec = $this->makeCodec( $lang );
238 $title = new TitleValue( $namespace, $dbkey, $fragment );
240 $actual = $codec->getPrefixedText( $title );
242 $this->assertEquals( $expected, $actual );
245 public static function provideGetPrefixedDBkey() {
247 [ NS_MAIN
, 'Foo_Bar', '', '', 'en', 'Foo_Bar' ],
248 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi_Maier' ],
250 // No capitalization or normalization is applied while formatting!
251 [ NS_USER_TALK
, 'hansi__maier', '', '', 'en', 'User_talk:hansi__maier' ],
253 // getGenderCache() provides a mock that considers first
254 // names ending in "a" to be female.
255 [ NS_USER
, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa_Müller' ],
257 [ NS_MAIN
, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ],
259 // non-existent namespace
260 [ 10000000, 'Foobar', '', '', 'en', 'Special:Badtitle/NS10000000:Foobar' ],
265 * @dataProvider provideGetPrefixedDBkey
267 public function testGetPrefixedDBkey( $namespace, $dbkey, $fragment,
268 $interwiki, $lang, $expected
270 $codec = $this->makeCodec( $lang );
271 $title = new TitleValue( $namespace, $dbkey, $fragment, $interwiki );
273 $actual = $codec->getPrefixedDBkey( $title );
275 $this->assertEquals( $expected, $actual );
278 public static function provideGetFullText() {
280 [ NS_MAIN
, 'Foo_Bar', '', 'en', 'Foo Bar' ],
281 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
283 // No capitalization or normalization is applied while formatting!
284 [ NS_USER_TALK
, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
289 * @dataProvider provideGetFullText
291 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
292 $codec = $this->makeCodec( $lang );
293 $title = new TitleValue( $namespace, $dbkey, $fragment );
295 $actual = $codec->getFullText( $title );
297 $this->assertEquals( $expected, $actual );
300 public static function provideParseTitle() {
301 // TODO: test capitalization and trimming
302 // TODO: test unicode normalization
305 [ ' : Hansi_Maier _ ', NS_MAIN
, 'en',
306 new TitleValue( NS_MAIN
, 'Hansi_Maier', '' ) ],
307 [ 'User:::1', NS_MAIN
, 'de',
308 new TitleValue( NS_USER
, '0:0:0:0:0:0:0:1', '' ) ],
309 [ ' lisa Müller', NS_USER
, 'de',
310 new TitleValue( NS_USER
, 'Lisa_Müller', '' ) ],
311 [ 'benutzerin:lisa Müller#stuff', NS_MAIN
, 'de',
312 new TitleValue( NS_USER
, 'Lisa_Müller', 'stuff' ) ],
314 [ ':Category:Quux', NS_MAIN
, 'en',
315 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
316 [ 'Category:Quux', NS_MAIN
, 'en',
317 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
318 [ 'Category:Quux', NS_CATEGORY
, 'en',
319 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
320 [ 'Quux', NS_CATEGORY
, 'en',
321 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
322 [ ':Quux', NS_CATEGORY
, 'en',
323 new TitleValue( NS_MAIN
, 'Quux', '' ) ],
325 // getGenderCache() provides a mock that considers first
326 // names ending in "a" to be female.
328 [ 'a b c', NS_MAIN
, 'en',
329 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
330 [ ' a b c ', NS_MAIN
, 'en',
331 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
332 [ ' _ Foo __ Bar_ _', NS_MAIN
, 'en',
333 new TitleValue( NS_MAIN
, 'Foo_Bar' ) ],
335 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
336 [ 'Sandbox', NS_MAIN
, 'en', ],
337 [ 'A "B"', NS_MAIN
, 'en', ],
338 [ 'A \'B\'', NS_MAIN
, 'en', ],
339 [ '.com', NS_MAIN
, 'en', ],
340 [ '~', NS_MAIN
, 'en', ],
341 [ '"', NS_MAIN
, 'en', ],
342 [ '\'', NS_MAIN
, 'en', ],
344 [ 'Talk:Sandbox', NS_MAIN
, 'en',
345 new TitleValue( NS_TALK
, 'Sandbox' ) ],
346 [ 'Talk:Foo:Sandbox', NS_MAIN
, 'en',
347 new TitleValue( NS_TALK
, 'Foo:Sandbox' ) ],
348 [ 'File:Example.svg', NS_MAIN
, 'en',
349 new TitleValue( NS_FILE
, 'Example.svg' ) ],
350 [ 'File_talk:Example.svg', NS_MAIN
, 'en',
351 new TitleValue( NS_FILE_TALK
, 'Example.svg' ) ],
352 [ 'Foo/.../Sandbox', NS_MAIN
, 'en',
354 [ 'Sandbox/...', NS_MAIN
, 'en',
356 [ 'A~~', NS_MAIN
, 'en',
358 // Length is 256 total, but only title part matters
359 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN
, 'en',
360 new TitleValue( NS_CATEGORY
,
361 'X' . str_repeat( 'x', 247 ) ) ],
362 [ str_repeat( 'x', 252 ), NS_MAIN
, 'en',
363 'X' . str_repeat( 'x', 251 ) ],
364 // Test decoding and normalization
365 [ '"ñ"', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, '"ñ"' ) ],
366 [ 'X#ñ', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'ñ' ) ],
367 // target section parsing
368 'empty fragment' => [ 'X#', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X' ) ],
369 'double hash' => [ 'X##', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', '#' ) ],
370 'fragment with hash' => [ 'X#z#z', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'z#z' ) ],
371 'fragment with space' => [ 'X#z z', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'z z' ) ],
372 'fragment with percent' => [ 'X#z%z', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'z%z' ) ],
373 'fragment with amp' => [ 'X#z&z', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'z&z' ) ],
378 * @dataProvider provideParseTitle
380 public function testParseTitle( $text, $ns, $lang, $title = null ) {
381 if ( $title === null ) {
382 $title = str_replace( ' ', '_', trim( $text ) );
385 if ( is_string( $title ) ) {
386 $title = new TitleValue( NS_MAIN
, $title, '' );
389 $codec = $this->makeCodec( $lang );
390 $actual = $codec->parseTitle( $text, $ns );
392 $this->assertEquals( $title, $actual );
395 public static function provideParseTitle_invalid() {
396 // TODO: test unicode errors
405 [ 'Talk:File:Foo.jpg' ],
406 [ 'Talk:localtestiw:Foo' ],
407 [ '::1' ], // only valid in user namespace
408 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
410 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
415 // Bad characters forbidden regardless of wgLegalTitleChars
427 // XML/HTML character entity references
428 // Note: Commented out because they are not marked invalid by the PHP test as
429 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
430 // [ 'A é B' ],
432 // [ 'A é B' ],
433 // Subject of NS_TALK does not roundtrip to NS_MAIN
434 [ 'Talk:File:Example.svg' ],
435 // Directory navigation
441 [ 'Foo/../Sandbox' ],
446 [ 'A ~~~~ Signature' ],
447 [ 'A ~~~~~ Timestamp' ],
448 [ str_repeat( 'x', 256 ) ],
449 // Namespace prefix without actual title
457 * @dataProvider provideParseTitle_invalid
459 public function testParseTitle_invalid( $text ) {
460 $this->setExpectedException( MalformedTitleException
::class );
462 $codec = $this->makeCodec( 'en' );
463 $codec->parseTitle( $text, NS_MAIN
);
467 * @dataProvider provideMakeTitleValueSafe
468 * @covers IP::sanitizeIP
470 public function testMakeTitleValueSafe(
471 $expected, $ns, $text, $fragment = '', $interwiki = '', $lang = 'en'
473 $codec = $this->makeCodec( $lang );
474 $this->assertEquals( $expected,
475 $codec->makeTitleValueSafe( $ns, $text, $fragment, $interwiki ) );
479 * @dataProvider provideMakeTitleValueSafe
480 * @covers Title::makeTitleSafe
481 * @covers Title::makeName
482 * @covers Title::secureAndSplit
483 * @covers IP::sanitizeIP
485 public function testMakeTitleSafe(
486 $expected, $ns, $text, $fragment = '', $interwiki = '', $lang = 'en'
488 $codec = $this->makeCodec( $lang );
489 $this->setService( 'TitleParser', $codec );
490 $this->setService( 'TitleFormatter', $codec );
492 $actual = Title
::makeTitleSafe( $ns, $text, $fragment, $interwiki );
493 // We need to reset some members so equality testing works
495 $wrapper = TestingAccessWrapper
::newFromObject( $actual );
496 $wrapper->mArticleID
= $actual->getNamespace() === NS_SPECIAL ?
0 : -1;
497 $wrapper->mLocalInterwiki
= false;
498 $wrapper->mUserCaseDBKey
= null;
500 $this->assertEquals( Title
::castFromLinkTarget( $expected ), $actual );
503 public static function provideMakeTitleValueSafe() {
505 'Nonexistent NS' => [ null, 942929, 'Test' ],
506 'Simple page' => [ new TitleValue( NS_MAIN
, 'Test' ), NS_MAIN
, 'Test' ],
509 'Passed fragment' => [
510 new TitleValue( NS_MAIN
, 'Test', 'Fragment' ),
511 NS_MAIN
, 'Test', 'Fragment'
513 'Embedded fragment' => [
514 new TitleValue( NS_MAIN
, 'Test', 'Fragment' ),
515 NS_MAIN
, 'Test#Fragment'
517 'Passed fragment with spaces' => [
518 // XXX Leading space is okay in fragment?
519 new TitleValue( NS_MAIN
, 'Test', ' Frag ment' ),
520 NS_MAIN
, ' Test ', " Frag_ment "
522 'Embedded fragment with spaces' => [
523 // XXX Leading space is okay in fragment?
524 new TitleValue( NS_MAIN
, 'Test', ' Frag ment' ),
525 NS_MAIN
, " Test # Frag_ment "
527 // XXX Is it correct that these aren't normalized to spaces?
528 'Passed fragment with leading tab' => [ null, NS_MAIN
, "\tTest\t", "\tFragment" ],
529 'Embedded fragment with leading tab' => [ null, NS_MAIN
, "\tTest\t#\tFragment" ],
530 'Passed fragment with trailing tab' => [ null, NS_MAIN
, "\tTest\t", "Fragment\t" ],
531 'Embedded fragment with trailing tab' => [ null, NS_MAIN
, "\tTest\t#Fragment\t" ],
532 'Passed fragment with interior tab' => [ null, NS_MAIN
, "\tTest\t", "Frag\tment" ],
533 'Embedded fragment with interior tab' => [ null, NS_MAIN
, "\tTest\t#\tFrag\tment" ],
536 'Passed local interwiki' => [
537 new TitleValue( NS_MAIN
, 'Test' ),
538 NS_MAIN
, 'Test', '', 'localtestiw'
540 'Embedded local interwiki' => [
541 new TitleValue( NS_MAIN
, 'Test' ),
542 NS_MAIN
, 'localtestiw:Test'
544 'Passed remote interwiki' => [
545 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
546 NS_MAIN
, 'Test', '', 'remotetestiw'
548 'Embedded remote interwiki' => [
549 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
550 NS_MAIN
, 'remotetestiw:Test'
552 // XXX Are these correct? Interwiki prefixes are case-sensitive?
553 'Passed local interwiki with different case' => [
554 new TitleValue( NS_MAIN
, 'LocalTestIW:Test' ),
555 NS_MAIN
, 'Test', '', 'LocalTestIW'
557 'Embedded local interwiki with different case' => [
558 new TitleValue( NS_MAIN
, 'LocalTestIW:Test' ),
559 NS_MAIN
, 'LocalTestIW:Test'
561 'Passed remote interwiki with different case' => [
562 new TitleValue( NS_MAIN
, 'RemoteTestIW:Test' ),
563 NS_MAIN
, 'Test', '', 'RemoteTestIW'
565 'Embedded remote interwiki with different case' => [
566 new TitleValue( NS_MAIN
, 'RemoteTestIW:Test' ),
567 NS_MAIN
, 'RemoteTestIW:Test'
569 'Passed local interwiki with lowercase page name' => [
570 new TitleValue( NS_MAIN
, 'Test' ),
571 NS_MAIN
, 'test', '', 'localtestiw'
573 'Embedded local interwiki with lowercase page name' => [
574 new TitleValue( NS_MAIN
, 'Test' ),
575 NS_MAIN
, 'localtestiw:test'
577 // For remote we don't auto-capitalize
578 'Passed remote interwiki with lowercase page name' => [
579 new TitleValue( NS_MAIN
, 'test', '', 'remotetestiw' ),
580 NS_MAIN
, 'test', '', 'remotetestiw'
582 'Embedded remote interwiki with lowercase page name' => [
583 new TitleValue( NS_MAIN
, 'test', '', 'remotetestiw' ),
584 NS_MAIN
, 'remotetestiw:test'
587 // Fragment and interwiki
588 'Fragment and local interwiki' => [
589 new TitleValue( NS_MAIN
, 'Test', 'Fragment' ),
590 NS_MAIN
, 'Test', 'Fragment', 'localtestiw'
592 'Fragment and remote interwiki' => [
593 new TitleValue( NS_MAIN
, 'Test', 'Fragment', 'remotetestiw' ),
594 NS_MAIN
, 'Test', 'Fragment', 'remotetestiw'
596 'Fragment and local interwiki and non-main namespace' => [
597 new TitleValue( NS_TALK
, 'Test', 'Fragment' ),
598 NS_TALK
, 'Test', 'Fragment', 'localtestiw'
600 // We don't know the foreign wiki's namespaces, so it will always be NS_MAIN
601 'Fragment and remote interwiki and non-main namespace' => [
602 new TitleValue( NS_MAIN
, 'Talk:Test', 'Fragment', 'remotetestiw' ),
603 NS_TALK
, 'Test', 'Fragment', 'remotetestiw'
606 // Whitespace normalization and Unicode stripping
607 'Name with space' => [
608 new TitleValue( NS_MAIN
, 'Test_test' ),
611 'Unicode bidi override characters' => [
612 new TitleValue( NS_MAIN
, 'Test' ),
613 NS_MAIN
, "\u{200E}T\u{200F}e\u{202A}s\u{202B}t\u{202C}\u{202D}\u{202E}"
615 'Invalid UTF-8 sequence' => [ null, NS_MAIN
, "Te\x80\xf0st" ],
616 'Whitespace collapsing' => [
617 new TitleValue( NS_MAIN
, 'Test_test' ),
618 NS_MAIN
, "Test _\u{00A0}\u{1680}\u{180E}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}" .
619 "\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}" .
620 "\u{205F}\u{3000}test"
622 'UTF8_REPLACEMENT' => [ null, NS_MAIN
, UtfNormal\Constants
::UTF8_REPLACEMENT
],
624 // Namespace prefixes
626 new TitleValue( NS_TALK
, 'Test' ),
629 'Test in talk NS' => [
630 new TitleValue( NS_TALK
, 'Test' ),
634 new TitleValue( NS_MAIN
, 'Talkk:Test' ),
635 NS_MAIN
, 'Talkk:Test'
637 'Talk:Talk:Test' => [ null, NS_MAIN
, 'Talk:Talk:Test' ],
638 'Talk:User:Test' => [ null, NS_MAIN
, 'Talk:User:Test' ],
639 'User:Talk:Test' => [
640 new TitleValue( NS_USER
, 'Talk:Test' ),
641 NS_MAIN
, 'User:Talk:Test'
643 'User:Test in talk NS' => [ null, NS_TALK
, 'User:Test' ],
644 'Talk:Test in talk NS' => [ null, NS_TALK
, 'Talk:Test' ],
645 'User:Test in user NS' => [
646 new TitleValue( NS_USER
, 'User:Test' ),
649 'Talk:Test in user NS' => [
650 new TitleValue( NS_USER
, 'Talk:Test' ),
656 new TitleValue( NS_MAIN
, 'Test' ),
660 new TitleValue( NS_TALK
, 'Test' ),
661 NS_MAIN
, ':Talk:Test'
663 ':localtestiw:Test' => [
664 new TitleValue( NS_MAIN
, 'Test' ),
665 NS_MAIN
, ':localtestiw:Test'
667 ':remotetestiw:Test' => [
668 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
669 NS_MAIN
, ':remotetestiw:Test'
671 // XXX Is this correct? Why is it different from remote?
672 'localtestiw::Test' => [ null, NS_MAIN
, 'localtestiw::Test' ],
673 'remotetestiw::Test' => [
674 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
675 NS_MAIN
, 'remotetestiw::Test'
677 // XXX Is this correct? Why is it different from remote?
678 'localtestiw:: Test' => [ null, NS_MAIN
, 'localtestiw:: Test' ],
679 'remotetestiw:: Test' => [
680 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
681 NS_MAIN
, 'remotetestiw:: Test'
685 'Empty title' => [ null, NS_MAIN
, '' ],
686 'Empty title with namespace' => [ null, NS_USER
, '' ],
687 'Local interwiki with empty page name' => [
688 new TitleValue( NS_MAIN
, 'Main_Page' ),
689 NS_MAIN
, 'localtestiw:'
691 'Remote interwiki with empty page name' => [
692 // XXX Is this correct? This is supposed to redirect to the main page remotely?
693 new TitleValue( NS_MAIN
, '', '', 'remotetestiw' ),
694 NS_MAIN
, 'remotetestiw:'
697 // Whitespace-only titles
698 'Whitespace-only title' => [ null, NS_MAIN
, "\t\n" ],
699 'Whitespace-only title with namespace' => [ null, NS_USER
, " _ " ],
700 'Local interwiki with whitespace-only page name' => [
701 // XXX Is whitespace-only really supposed to be different from empty?
703 NS_MAIN
, "localtestiw:_\t"
705 'Remote interwiki with whitespace-only page name' => [
706 // XXX Is whitespace-only really supposed to be different from empty?
708 NS_MAIN
, "remotetestiw:\t_\n\r"
711 // Namespace and interwiki
712 'Talk:localtestiw:Test' => [ null, NS_MAIN
, 'Talk:localtestiw:Test' ],
713 'Talk:remotetestiw:Test' => [ null, NS_MAIN
, 'Talk:remotetestiw:Test' ],
714 'User:localtestiw:Test' => [
715 new TitleValue( NS_USER
, 'Localtestiw:Test' ),
716 NS_MAIN
, 'User:localtestiw:Test'
718 'User:remotetestiw:Test' => [
719 new TitleValue( NS_USER
, 'Remotetestiw:Test' ),
720 NS_MAIN
, 'User:remotetestiw:Test'
722 'localtestiw:Test in user namespace' => [
723 new TitleValue( NS_USER
, 'Localtestiw:Test' ),
724 NS_USER
, 'localtestiw:Test'
726 'remotetestiw:Test in user namespace' => [
727 new TitleValue( NS_USER
, 'Remotetestiw:Test' ),
728 NS_USER
, 'remotetestiw:Test'
730 'localtestiw:talk:test' => [
731 new TitleValue( NS_TALK
, 'Test' ),
732 NS_MAIN
, 'localtestiw:talk:test'
734 'remotetestiw:talk:test' => [
735 new TitleValue( NS_MAIN
, 'talk:test', '', 'remotetestiw' ),
736 NS_MAIN
, 'remotetestiw:talk:test'
740 'Test[test' => [ null, NS_MAIN
, 'Test[test' ],
743 '255 chars long' => [
744 new TitleValue( NS_MAIN
, str_repeat( 'A', 255 ) ),
745 NS_MAIN
, str_repeat( 'A', 255 )
747 '255 chars long in user NS' => [
748 new TitleValue( NS_USER
, str_repeat( 'A', 255 ) ),
749 NS_USER
, str_repeat( 'A', 255 )
751 'User:255 chars long' => [
752 new TitleValue( NS_USER
, str_repeat( 'A', 255 ) ),
753 NS_MAIN
, 'User:' . str_repeat( 'A', 255 )
755 '256 chars long' => [ null, NS_MAIN
, str_repeat( 'A', 256 ) ],
756 '256 chars long in user NS' => [ null, NS_USER
, str_repeat( 'A', 256 ) ],
757 'User:256 chars long' => [ null, NS_MAIN
, 'User:' . str_repeat( 'A', 256 ) ],
759 '512 chars long in special NS' => [
760 new TitleValue( NS_SPECIAL
, str_repeat( 'A', 512 ) ),
761 NS_SPECIAL
, str_repeat( 'A', 512 )
763 'Special:512 chars long' => [
764 new TitleValue( NS_SPECIAL
, str_repeat( 'A', 512 ) ),
765 NS_MAIN
, 'Special:' . str_repeat( 'A', 512 )
767 '513 chars long in special NS' => [ null, NS_SPECIAL
, str_repeat( 'A', 513 ) ],
768 'Special:513 chars long' => [ null, NS_MAIN
, 'Special:' . str_repeat( 'A', 513 ) ],
771 'User:000.000.000' => [
772 new TitleValue( NS_USER
, '000.000.000' ),
773 NS_MAIN
, 'User:000.000.000'
775 'User:000.000.000.000' => [
776 new TitleValue( NS_USER
, '0.0.0.0' ),
777 NS_MAIN
, 'User:000.000.000.000'
779 '000.000.000.000' => [
780 new TitleValue( NS_MAIN
, '000.000.000.000' ),
781 NS_MAIN
, '000.000.000.000'
783 'User:1.1.256.000' => [
784 new TitleValue( NS_USER
, '1.1.256.000' ),
785 NS_MAIN
, 'User:1.1.256.000'
787 'User:1.1.255.000' => [
788 new TitleValue( NS_USER
, '1.1.255.0' ),
789 NS_MAIN
, 'User:1.1.255.000'
791 // TODO More IP address sanitization tests
794 // Invalid and valid dots
795 foreach ( [ '.', '..', '...' ] as $dots ) {
796 foreach ( [ '?', '?/', '?/Test', 'Test/?/Test', '/?', 'Test/?', '?Test', 'Test?Test',
797 'Test?' ] as $pattern ) {
798 $test = str_replace( '?', $dots, $pattern );
799 if ( $dots === '...' ||
in_array( $pattern, [ '?Test', 'Test?Test', 'Test?' ] ) ) {
800 $expectedMain = new TitleValue( NS_MAIN
, $test );
801 $expectedUser = new TitleValue( NS_USER
, $test );
803 $expectedMain = $expectedUser = null;
805 $ret[$test] = [ $expectedMain, NS_MAIN
, $test ];
806 $ret["$test in user NS"] = [ $expectedUser, NS_USER
, $test ];
807 $ret["User:$test"] = [ $expectedUser, NS_MAIN
, "User:$test" ];
811 // Invalid and valid tildes
812 foreach ( [ '~~', '~~~' ] as $tildes ) {
813 foreach ( [ '?', 'Test?', '?Test', 'Test?Test' ] as $pattern ) {
814 $test = str_replace( '?', $tildes, $pattern );
815 if ( $tildes === '~~' ) {
816 $expectedMain = new TitleValue( NS_MAIN
, $test );
817 $expectedUser = new TitleValue( NS_USER
, $test );
819 $expectedMain = $expectedUser = null;
821 $ret[$test] = [ $expectedMain, NS_MAIN
, $test ];
822 $ret["$test in user NS"] = [ $expectedUser, NS_USER
, $test ];
823 $ret["User:$test"] = [ $expectedUser, NS_MAIN
, "User:$test" ];
830 public static function provideGetNamespaceName() {
832 [ NS_MAIN
, 'Foo', 'en', '' ],
833 [ NS_USER
, 'Foo', 'en', 'User' ],
834 [ NS_USER
, 'Hansi Maier', 'de', 'Benutzer' ],
836 // getGenderCache() provides a mock that considers first
837 // names ending in "a" to be female.
838 [ NS_USER
, 'Lisa Müller', 'de', 'Benutzerin' ],
843 * @dataProvider provideGetNamespaceName
845 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
846 $codec = $this->makeCodec( $lang );
847 $name = $codec->getNamespaceName( $namespace, $text );
849 $this->assertEquals( $expected, $name );